
//arrayBuffer转16进制字符串
function ab2hex(buffer) {
	const hexArr = Array.prototype.map.call(
		new Uint8Array(buffer),
		function(bit) {
			return ('00' + bit.toString(16)).slice(-2)
		}
	)
	return hexArr.join('');
}


//解包
function solutionData(str, len) {
	str = str.toUpperCase();
	var packages = {};
	packages.top = str.substr(0, 2);
	packages.command = str.substr(2, 2);
	if (packages.command == '03') {
		var length = len * 2;
		packages.length = parseInt('0x' + str.substr(4, 2));
		if (packages.length != length) {
			return undefined;
		}
		var data = str.substr(6, length * 2);
		packages.data = data;
	} else if (packages.command == '06') {
		packages.start = str.substr(4, 4);
		packages.data = str.substr(8, 4);
	}
	packages.crc16 = str.substr(str.length - 4, 4);
	return packages;
}

//16进制转明文
function algorithm(address, v1) {
	address = address.toUpperCase();
	var value = 0;
	switch (address) {
		case '1003':
		case '1011':
		case '1012':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 100).toFixed(1));
			break;
		case '1004':
			//console.log(v1, '1004');
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10 - 1000).toFixed(1));
			break;
		case '1005':
		case '1006':
		case '1007':
		case '1008':
		case '1009':
		case '100A':
		case '100B':
		case '100C':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10 - 40).toFixed(1));
			break;
		case '1014':
		case '1016':
			var v = parseInt("0x" + v1).toString(2);
			var vArray = v.split('').reverse();
			var result = [];
			for (var i = 0; i < 16; i++) {
				var item = vArray[i];
				if (item) {
					result.push(item);
				} else {
					result.push(0);
				}
			}
			return result;
			break;
		case '100F':
			//console.log(v1);
			var h = parseInt('0x' + v1.substr(0, 2));
			var l = parseInt('0x' + v1.substr(2, 4));
			//console.log(name, h, l);
			var result = [h, l];
			return result;
			break;

			//保护参数寄存器
		case '2300': //电压V算法
		case '2302':
		case '2308':
		case '230A':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 100).toFixed(2));
			break;
		case '2310': //温度算法
		case '2312':
		case '2314':
		case '2316':
		case '2318':
		case '231A':
		case '231C':
		case '231E':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10 - 40).toFixed(1));
			break;
		case '2320': //电流A算法
		case '2322':
		case '2324':
		case '2326':
		case '2328':
		case '232A':
		case '232C':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10).toFixed(1));
			break;

			//硬件参数配置
		case '210A':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 100).toFixed(2));
			break;
		
		case '2110':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10).toFixed(1));
			break;
	
		case '2113':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10).toFixed(1));
			break;

		case '2114':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 1000).toFixed(3));
			break;

			//校准参数配置
		case '2411':
			var v = parseInt("0x" + v1);
			value = v - 1000;
			break;
		case '2420':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 100).toFixed(2));
			break;
		case '2427':
			var v = parseInt("0x" + v1);
			value = parseFloat((v / 10).toFixed(1));
			break;

		default:
			var v = parseInt("0x" + v1);
			value = v;
			break;
	}
	return value;
}

//明文转16进制
function encryption(address, v1) {
	address = address.toUpperCase();
	switch (address) {
		//保护参数寄存器
		case '2300': //电压V算法
		case '2302':
		case '2308':
		case '230A':
			v1 = v1 * 100;
			break;
		case '2310': //温度算法
		case '2312':
		case '2314':
		case '2316':
		case '2318':
		case '231A':
		case '231C':
		case '231E':
			v1 = v1 * 10 + 400;
			break;
		case '2320': //电流A算法
		case '2322':
		case '2324':
		case '2326':
		case '2328':
		case '232A':
		case '232C':
			v1 = v1 * 10;
			break;

			//硬件参数配置
		case '210A':
			v1 = v1 * 100;
			break;
		
		case '2110':
			v1 = v1 * 10;
			break;

		case '2113':
			v1 = v1 * 10;
			break;

		case '2114':
			v1 = v1 * 1000;
			break;

			//校准参数配置
		case '2411':
			v1 = v1 + 1000;
			break;
		case '2420':
			v1 = v1 * 100;
			break;
		case '2427':
			v1 = v1 * 10;
			break;
		default:

			break;
	}
	var v2 = v1.toString(16);
	if (v2 == '') {
		return '';
	}
	if (v2.indexOf('-') >= 0) {
		return '';
	}
	if (v2.length == 1) {
		return '000' + v2;
	} else if (v2.length == 2) {
		return '00' + v2;
	} else if (v2.length == 3) {
		return '0' + v2;
	} else if (v2.length == 4) {
		return v2;
	} else {
		return '';
	}
}

export default {
	bufferToHex: ab2hex,
	solutionData: solutionData,
	algorithm: algorithm,
}
